package com.yiyi.credit.manage.sso.service.inner;


import com.yiyi.credit.manage.sso.common.Constant;
import com.yiyi.credit.manage.sso.common.exception.ResponseCode;
import com.yiyi.credit.manage.sso.common.exception.ServiceException;
import com.yiyi.credit.manage.sso.mapper.SysResourceMapper;
import com.yiyi.credit.manage.sso.model.entity.SysResource;
import com.yiyi.credit.manage.sso.model.entity.SysUserRole;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class UserPermissionServiceImpl implements UserPermissionService {


    @Autowired
    SysUserRoleService sysUserRoleService;

    @Autowired
    SysResourceMapper sysResourceMapper;

    @Autowired
    SysResourceService sysResourceService;


    @Override
    public List<SysResource> getSysUserPermission(Long userId, Long sysInfoId) throws ServiceException {
        //查询用户角色
        SysUserRole sysUserRole = getSysUserInfo(userId, sysInfoId);

        return sysResourceMapper.getUserRoleSysResource(sysUserRole.getRoleId(), sysInfoId);
    }


    @Override
    public SysResource getSysChildNodePermission(Long userId, Long sysInfoId, Long pid,
                                                 boolean whetherAllChild) throws ServiceException {
        //查询用户角色
        SysUserRole sysUserRole = getSysUserInfo(userId, sysInfoId);

        SysResource sysResource = new SysResource();
        if (pid.equals(Constant.ROOT_PID_CODE)) {
            sysResource.setId(pid);
            sysResource.setSysInfoId(sysInfoId);
        } else {
            sysResource = sysResourceMapper.selectByPrimaryKey(pid);
        }

        List<SysResource> sysResourceList = sysResourceMapper.getUserRoleSysResourceByPid(
            sysUserRole.getRoleId(), sysInfoId, pid);
        if (whetherAllChild) {
            buildAllChildNodePermission(sysResourceList, sysResource, sysUserRole.getRoleId(), sysInfoId);
        } else {
            sysResource.setSysChildResourceList(sysResourceList);
        }
        return sysResource;
    }


    @Override
    public List<SysResource> gerSysResourceBySysInfoId(Long sysInfoId) throws ServiceException {
        return sysResourceService.getSysResourceBySysId(sysInfoId);
    }


    @Override
    public SysResource getSysUserPermissionByUrl(Long userId, Long sysInfoId, String url) throws ServiceException {

        //查询用户角色
        SysUserRole sysUserRole = getSysUserInfo(userId, sysInfoId);
        List<SysResource> sysResourceList = sysResourceMapper.getSysUserPermissionByUrl(sysUserRole.getRoleId(),
            sysInfoId, url);
        if (null == sysResourceList || sysResourceList.size() == Constant.INT_ZERO) {
            throw new ServiceException(ResponseCode.RESOURCE_NOT_FUND);
        }
        return sysResourceList.get(Constant.INT_ZERO);
    }


    private SysUserRole getSysUserInfo(Long userId, Long sysInfoId) throws ServiceException {
        List<SysUserRole> sysUserRoleList = sysUserRoleService.getSysUserRole(userId, sysInfoId);
        if (null == sysUserRoleList || sysUserRoleList.size() == Constant.INT_ZERO) {
            throw new ServiceException(ResponseCode.USER_ROLE_NOT_EXIST);
        }
        return sysUserRoleList.get(Constant.INT_ZERO);
    }


    private void buildAllChildNodePermission(List<SysResource> sysResourceList, SysResource returnResource, Long roleId,
                                             Long sysInfoId) throws ServiceException {

        if (null != sysResourceList && sysResourceList.size() > Constant.INT_ZERO) {
            for (SysResource sysResource : sysResourceList) {
                List<SysResource> childResourceList = sysResourceMapper.getUserRoleSysResourceByPid(roleId, sysInfoId,
                    sysResource.getId());
                if (null != childResourceList && childResourceList.size() > Constant.INT_ZERO) {
                    buildAllChildNodePermission(childResourceList, sysResource, roleId, sysInfoId);
                }
            }
            returnResource.setSysChildResourceList(sysResourceList);
        }
    }

}
